#!/usr/bin/env python

#this extracts out all the map tiles that are recorded in the phone.
#THe concept is taken from Oreilly.iPhone.Forensics.Sep.2008

import sqlite3, os

def getTableDump(db_file, table_to_dump):
    conn = sqlite3.connect(':memory:')    
    cu = conn.cursor()
    cu.execute("attach database '" + db_file + "' as attached_db")
    cu.execute("select sql from attached_db.sqlite_master "
               "where type='table' and name='" + table_to_dump + "'")
    sql_create_table = cu.fetchone()[0]
    cu.execute(sql_create_table);
    cu.execute("insert into " + table_to_dump +
               " select * from attached_db." + table_to_dump)
    conn.commit()
    cu.execute("detach database attached_db")
    return "\n".join(conn.iterdump())



def main():
	#now we need to dump images to MapTiles.sql

	#the getTableDump gets the .output of a table and returns it
	TABLE_TO_DUMP = 'images'
	DB_FILE = 'MapTiles.sqlitedb'
	f= open('MapTiles.sql','w')

	#we capture that and divert it to a sql file
	f.write(getTableDump(DB_FILE, TABLE_TO_DUMP))

	#now we run the perl script to retrieve all the maptiles from the dump
	os.system("perl parse_maptiles.pl MapTiles.sql")



#exec main
if __name__ == '__main__':
	main()